﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace CMS_GYM
{
    public class clsPhieuThuTienChiTietDAO
    {
        #region Attribute
        /// <summary>
        /// Kết nối SQL Server
        /// </summary>
        private CSQLServer sqlServer = new CSQLServer();
        #endregion

        public DataTable LayBang(string SoPhieuThu, string KyHieu)
        {
            string sql = "spGetInfoPhieuThuChiTietTheoSoPhieuThuVaKyHieu";
            string[] ParameterColection = new string[] {    
                                    "@SoPhieuThu","@KyHieu"
                                };
            Object[] valueofParameter = new Object[] {
                                    SoPhieuThu,KyHieu
                                };
            DataTable table = sqlServer.readData(sql, ParameterColection, valueofParameter);
            return table;
        }

        public DataTable LayBang(decimal PhieuThuId)
        {
            string sql = "spGetBangPhieuThuChiTiet";
            string[] ParameterColection = new string[] {"@PhieuThuId"};
            Object[] valueofParameter = new Object[] { PhieuThuId };
            DataTable table = sqlServer.readData(sql, ParameterColection, valueofParameter);
            return table;
        }

        /// <summary>
        /// Lay danh sach phieu thu chi tiet theo phieu thu id
        /// </summary>
        /// <param name="PhieuThuId"></param>
        /// <returns></returns>
        public List<clsPhieuThuTienChiTietDTO> LayDanhSach(decimal PhieuThuId)
        {
            List<clsPhieuThuTienChiTietDTO> CacPTCT = new List<clsPhieuThuTienChiTietDTO>();
            DataTable table = LayBang(PhieuThuId);
            if (table == null)
            {
                return null;
            }
            else
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    clsPhieuThuTienChiTietDTO ptct = ChuyenDoi(table.Rows[i]);
                    if (ptct == null)
                    {
                        return null;
                    }
                    else
                    {
                        CacPTCT.Add(ptct);
                    }
                }
                return CacPTCT;
            }
        }

        /// <summary>
        /// Lay danh sach phieu thu chi tiet theo so phieu thu va ky hieu
        /// </summary>
        /// <param name="SoPhieuThu"></param>
        /// <param name="KyHieu"></param>
        /// <returns></returns>
        public List<clsPhieuThuTienChiTietDTO> LayDanhSach(string SoPhieuThu, string KyHieu)
        {
            List<clsPhieuThuTienChiTietDTO> CacPTCT = new List<clsPhieuThuTienChiTietDTO>();
            DataTable table = LayBang(SoPhieuThu,KyHieu);
            if (table == null)
            {
                return null;
            }
            else
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    clsPhieuThuTienChiTietDTO ptct = ChuyenDoi(table.Rows[i]);
                    if (ptct == null)
                    {
                        return null;
                    }
                    else
                    {
                        CacPTCT.Add(ptct);
                    }
                }
                return CacPTCT;
            }
        }

        private clsPhieuThuTienChiTietDTO ChuyenDoi(DataRow row)
        {
            clsPhieuThuTienChiTietDTO PhieuThu = new clsPhieuThuTienChiTietDTO();
            try
            {
                PhieuThu.PhieuThuId = decimal.Parse(row["PhieuThuId"].ToString());
                PhieuThu.PhieuThuChiTietId = decimal.Parse(row["PhieuThuChiTietId"].ToString());
                PhieuThu.NoiDungThu = row["NoiDungThu"] == null ? string.Empty : row["NoiDungThu"].ToString();
                PhieuThu.TienThu = double.Parse(row["TienThu"].ToString());
                PhieuThu.TienHang = double.Parse(row["TienHang"].ToString());
                PhieuThu.TienThue = double.Parse(row["TienThue"].ToString());
                PhieuThu.ChietKhau = double.Parse(row["ChietKhau"].ToString());
                PhieuThu.TaiKhoanCo = row["TaiKhoanCo"] == null ? string.Empty : row["TaiKhoanCo"].ToString();
                PhieuThu.TaiKhoanNo = row["TaiKhoanNo"] == null ? string.Empty : row["TaiKhoanNo"].ToString();
                PhieuThu.TheId = row["TheId"] == null || string.IsNullOrEmpty(row["TheId"].ToString()) ? null : (decimal?)decimal.Parse(row["TheId"].ToString());

                return PhieuThu;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Thêm thông tin chi tiết phiếu xuất 
        /// </summary>
        /// <param name="ChiTietPhieuXuất">
        /// MaPhieuXuat  nvarchar(10)
        /// MaMatHang  nvarchar(10)
        /// SoLuong  int
        /// DonGia  float
        /// ChietKhau  float
        /// ThanhTien  float
        /// ThueVAT  float
        /// </param>
        public int Them(clsPhieuThuTienChiTietDTO PTCT)
        {
            int i = -1;
            string sql = "spInsertPhieuThuChiTiet";
            string[] ParameterColection = new string[] {    
                                    "@PhieuThuId","@NoiDungThu","@TienThu","@TienHang",
                                    "@TienThue","@ChietKhau","@TaiKhoanNo","@TaiKhoanCo","@TheId"
                                };
            Object[] valueofParameter = new Object[] {
                                   PTCT.PhieuThuId, PTCT.NoiDungThu, PTCT.TienThu,PTCT.TienHang,
                                   PTCT.TienThue, PTCT.ChietKhau, PTCT.TaiKhoanNo, PTCT.TaiKhoanCo,PTCT.TheId
                                };

            i = sqlServer.writeData(sql, ParameterColection, valueofParameter);
            return i;
        }

        /// <summary>
        /// Sua mot phieu thu chi tiêt
        /// </summary>
        /// <param name="PTCT"></param>
        /// <returns></returns>
        public int Sua(clsPhieuThuTienChiTietDTO PTCT)
        {
            int i = -1;
            string sql = "spUpdatePhieuThuChiTiet";
            string[] ParameterColection = new string[] {    
                                    "@PhieuThuChiTietId","@NoiDungThu","@TienThu","@TienHang",
                                    "@TienThue","@ChietKhau","@TaiKhoanNo","@TaiKhoanCo"
                                };
            Object[] valueofParameter = new Object[] {
                                   PTCT.PhieuThuChiTietId, PTCT.NoiDungThu, PTCT.TienThu,PTCT.TienHang,
                                   PTCT.TienThue, PTCT.ChietKhau, PTCT.TaiKhoanNo, PTCT.TaiKhoanCo
                                };

            i = sqlServer.writeData(sql, ParameterColection, valueofParameter);
            return i;
        }

        /// <summary>
        /// Xoa mot phieu thu chi tiêt
        /// </summary>
        /// <param name="PTCT"></param>
        /// <returns></returns>
        public int Xoa(decimal PhieuThuId)
        {
            int i = -1;
            string sql = "spDeletePhieuThuChiTiet";
            string[] ParameterColection = new string[] {    "@PhieuThuId"};
            Object[] valueofParameter = new Object[] { PhieuThuId };

            i = sqlServer.writeData(sql, ParameterColection, valueofParameter);
            return i;
        }

    }
}
